package com.atstudy.service;

import com.atstudy.utils.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/1/12 10:47
 * @copyright: 博为峰
 */
public class AccountServiceImpl implements AccountService {

    @Override
    public int transfer(String from, String to, int money) {
        AccountDaoImpl accountDao = new AccountDaoImpl();
        Connection conn = null;
        try {
            conn = DBUtils.getConn();
            conn.setAutoCommit(false);
            //增
            accountDao.add(money, from);
            //减
            accountDao.sub(money, to);
            conn.commit();
            return 1;
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }finally {
            try {
                DBUtils.closeConn(conn);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return -1;
    }
}
